//
//  MainTabBarController.m
//  基本框架
//
//  Created by runone02 on 2018/6/26.
//  Copyright © 2018年 liqiyun. All rights reserved.
//

#import "MainTabBarController.h"
#import "BaseNavigationController.h"
#import "ZMHomeViewController.h"
#import "ZMDiscoverViewController.h"
#import "MineController.h"
#import "ZMTopicViewController.h"
#import <AudioToolbox/AudioToolbox.h>
#import "ZMMessageViewController.h"
#import "LoginViewController.h"
#import "MultiRowsVC.h"
#import "NormalListController.h"
#import "RollingViewController.h"
@interface MainTabBarController ()

@end

@implementation MainTabBarController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self addChildVC];
}


- (void) addChildVC {
    ZMHomeViewController * fate = [[ZMHomeViewController alloc]init];
    [self addChildVC:fate title:@"首页" norImage:@"tabbar_icon0" highlightImage:@"tabbar_icon0_s"];
    
    ZMDiscoverViewController * letter = [[ZMDiscoverViewController alloc]init];
    [self addChildVC:letter title:@"发现" norImage:@"tabbar_icon1" highlightImage:@"tabbar_icon1_s"];
    
//    ZMTopicViewController * nearby = [[ZMTopicViewController alloc]init];
//    [self addChildVC:nearby title:@"话题" norImage:@"tabbar_icon2" highlightImage:@"tabbar_icon2_s"];
    MultiRowsVC *multi = [[MultiRowsVC alloc]init];
    [self addChildVC:multi title:@"话题" norImage:@"tabbar_icon2" highlightImage:@"tabbar_icon2_s"];
    
//        NormalListController *multi = [[NormalListController alloc]init];
//        [self addChildVC:multi title:@"话题" norImage:@"tabbar_icon2" highlightImage:@"tabbar_icon2_s"];
    
    RollingViewController *message = [[RollingViewController alloc]init];
    [self addChildVC:message title:@"消息" norImage:@"tabbar_icon_message" highlightImage:@"tabbar_icon_message_s"];
    
    MineController *mine = [[MineController alloc]init];
    [self addChildVC:mine  title:@"我的" norImage:@"tabbar_mine" highlightImage:@"tabbar_mine_select"];

}

+ (void)initialize {
    //获取UITbaBarItem外观对象不能使用UIBarButtonItem外观对象
    UITabBarItem *tabbarItem = [UITabBarItem appearance];
    //设置tabbar按钮文字渲染颜色
    NSDictionary *norDict = @{
                              NSForegroundColorAttributeName : [UIColor lightGrayColor],
                              NSFontAttributeName : [UIFont systemFontOfSize:14]
                              };
    [tabbarItem setTitleTextAttributes:norDict forState:UIControlStateNormal];
    
    //高亮状态
    NSDictionary *selectedDict = @{
                                   NSForegroundColorAttributeName : [UIColor blueColor],
                                   NSFontAttributeName : [UIFont systemFontOfSize:14.0]
                                   };
    [tabbarItem setTitleTextAttributes:selectedDict forState:UIControlStateSelected];
    
}


/**
 添加子控制器

 @param chinldVC 子控制器
 @param title 子控制器标题
 @param norImage 默认状态下图片
 @param highlightImage 高亮状态下图片
 */
- (void) addChildVC : (UIViewController *) chinldVC title: (NSString *)title norImage: (NSString *)norImage highlightImage: (NSString *) highlightImage{
    
    //设置子控制器标题
    chinldVC.navigationItem.title = title;
    chinldVC.tabBarItem.title = title;
    
    //设置tabbar按钮图片
    [chinldVC.tabBarItem setImage:[UIImage imageNamed:norImage]];
    [chinldVC.tabBarItem setSelectedImage:[UIImage imageNamed:highlightImage]];
    
    //导航控制器
    BaseNavigationController *nav = [[BaseNavigationController alloc]initWithRootViewController:chinldVC];
    
    [self addChildViewController:nav];
}

/**  */

- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item{
    NSInteger index = [self.tabBar.items indexOfObject:item];
    [self playSound];
    [self animationWhithIndex:index];
}


/**
 点击时的声效
 */
- (void) playSound {
//    NSString *path = [[NSBundle mainBundle] pathForResource:@"like" ofType:@"caf"];
//    
//    SystemSoundID soundID;
    
//    NSURL *soundURL = [NSURL fileURLWithPath:path];
//    AudioServicesCreateSystemSoundID((__bridge CFURLRef)soundURL,&soundID);
//    AudioServicesPlaySystemSound(soundID);
}

- (void)animationWhithIndex:(NSInteger) index{
    if (self.selectedIndex == index) {
        return;
    }
    NSMutableArray *tabbarbuttonArray = [NSMutableArray array];
    for (UIView *tabBarButton in self.tabBar.subviews) {
        if ([tabBarButton isKindOfClass:NSClassFromString(@"UITabBarButton")]) {
            [tabbarbuttonArray addObject:tabBarButton];
        }
    }
    CATransition *animtion = [CATransition animation];
    [animtion setDuration:0.3f];
    
    //切换动画风格
    [animtion setType:kCATransitionFade];
    
    //动画切换方向
    [animtion setSubtype:kCATransitionFromBottom];
    [animtion setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];
    
    [[tabbarbuttonArray[index] layer]addAnimation:animtion forKey:@"UITabBarButton.transform.scale"];
}
@end
